// PumaBmpWnd.cpp : implementation file
//

#include "stdafx.h"
#include "PumaBmpWnd.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPumaBmpWnd

CPumaBmpWnd::CPumaBmpWnd()
{
	m_bAutoSize = FALSE;
	m_bTile = FALSE;
}

CPumaBmpWnd::~CPumaBmpWnd()
{
}


BEGIN_MESSAGE_MAP(CPumaBmpWnd, CWnd)
	//{{AFX_MSG_MAP(CPumaBmpWnd)
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CPumaBmpWnd message handlers


BOOL CPumaBmpWnd::LoadSkin(UINT nID)
{
	if(!m_dcSkin.CreateDCFromBmp(nID))
		return FALSE;

	Invalidate();
	return TRUE;
}

BOOL CPumaBmpWnd::SetAutoSize(BOOL bAutoSize)
{
	if(!GetSafeHwnd())
		return FALSE;

	if(!m_bAutoSize && bAutoSize)
	{
		GetClientRect(m_rcPrev);

		SetWindowPos(NULL, 0, 0, m_dcSkin.Width(),
			m_dcSkin.Height(),
			SWP_NOZORDER | SWP_NOMOVE);
	}
	else if(m_bAutoSize && !bAutoSize)
	{
		SetWindowPos(NULL, 0, 0, m_rcPrev.Width(),
			m_rcPrev.Height(),
			SWP_NOZORDER | SWP_NOMOVE);
	}

	m_bAutoSize = bAutoSize;
	return TRUE;
}
BOOL CPumaBmpWnd::SetTile(BOOL bTile)
{
	m_bTile = bTile;
	Invalidate();
	return TRUE;
}

void CPumaBmpWnd::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	// Do not call CWnd::OnPaint() for painting messages

	DrawBKSkin(&dc);
}


BOOL CPumaBmpWnd::DrawBKSkin(CDC* pDC)
{
	safe_assert(pDC);
	if(!m_dcSkin.GetSafeHdc())
		return FALSE;

	CRect rect;
	GetClientRect(rect);
#ifdef _USE_HALFTONE
	pDC->SetStretchBltMode(HALFTONE);
#endif
	if(m_bTile)
	{
		// tile the bitmap
		int x = 0, y = 0;
		while(y < rect.Height())
		{
			while(x < rect.Width())
			{
				pDC->BitBlt(x, y, m_dcSkin.Width(), m_dcSkin.Height(), 
					&m_dcSkin, 0, 0, SRCCOPY);

				x += m_dcSkin.Width();
			}
			x = 0;
			y += m_dcSkin.Height();
		}
	}
	else
	{
		pDC->StretchBlt(0, 0, rect.Width(), rect.Height(), 
			&m_dcSkin, 0, 0,m_dcSkin.Width(), 
			m_dcSkin.Height(), SRCCOPY);
	}
	return TRUE;
}

